/*
* 多表join查询类
*/
import {isEmpty, isString, assert} from "../public";

export default class JoinBuilder {
    table;//表名
    primaryKey;//字段
    type;//类型
    //所有操作符
    static EXPRS = {
      app:"@",
      left:"<",
      right:">",
      inner:"&",
      full:"|",
      outer:"!",
      cross:"*",
      side:"^",
      anti:"(",
      foreign:")"
    };
    constructor({table,primaryKey=null,type=null}) {
      //判断参数是否合法
        assert(!isEmpty(table) && isString(table), `[ApiJson] ${table} 非法`);
        assert(!isEmpty(type) && isString(type), `[ApiJson] ${type} 非法`);
        //赋值给this
        this.table = table;
        this.primaryKey =primaryKey? primaryKey+"@":null;
        this.type = type;
    }

    static by(jsonData) {
        return new JoinBuilder(jsonData);
    }

    toJson() {
      let json = "";
      const { table, primaryKey, type } = this;
      //判断是否有参数 如果有的话就拼接  "&/slaveTable1/id@,</slaveTable2"
      if (primaryKey) {
        json = `${JoinBuilder.EXPRS[type]}/${table}/${primaryKey}`;
      } else {
        json = `${JoinBuilder.EXPRS[type]}/${table}`;
      }
      return json;
    }
}